package org.cwnuit.train.acton;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.cwnuit.train.domain.Learns;
import org.cwnuit.train.domain.Rules;
import org.cwnuit.train.domain.Sworks;
import org.cwnuit.train.domain.Trains;
import org.cwnuit.train.domain.Users;
import org.cwnuit.train.service.RulesService;
import org.cwnuit.train.service.SworksService;
import org.cwnuit.train.service.TrainsService;
import org.cwnuit.train.utils.PageModel;
import org.cwnuit.train.utils.Today;
import org.cwnuit.train.vo.SworkRuleVO;

import com.sun.corba.se.spi.orbutil.fsm.Input;

@SuppressWarnings("all")
public class SworksAction extends HttpServlet {

	SworksService sworksServices = new SworksService();
	RulesService rulesServices = new RulesService();
	TrainsService trainsServices = new TrainsService();
	
	protected void service(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		
		String cmd = request.getParameter("cmd");
		
		if (cmd.equals("update")) {// 修改信息
			int rid = Integer.parseInt(request.getParameter("rid"));
			String tid = request.getParameter("tid");
			String msg = "";
			String sid = request.getParameter("sid");
			String action = request.getParameter("action");
			if (sid.equals("0")||sid.equals("")|| sid == null) {
				if (action.equals("scontent")) {
					this.addSworkScontent(request);
					msg = "恭喜你，你已经成功录入了车站的规章制度！";
				} else if(action.equals("scommon")){
					this.addSworkScommon(request);
					msg = "恭喜你，你已经成功录入了车站规章制度的参考章程！";
				}
			} else {
				if (action.equals("scontent")) {
					this.updateSworkScontent(request);
					msg = "恭喜你，你已经成功修改了车站的规章制度！";
				} else if(action.equals("scommon")){
					this.updateSworkScommon(request);
					msg = "恭喜你，你已经成功修改了车站规章制度的参考章程！！";
				} else if(action.equals("ssstate")){
					this.updateSworkSsstate(request);
					msg = "恭喜你，你已经成功审核了该片规章制度！！";
				} else if(action.equals("sdstate")){
					this.updateSworkSdstate(request);
					msg = "恭喜你，你已经成功审核了该片规章制度！！";
				}
			}
			request.getSession().setAttribute("msg", msg);
			response.sendRedirect("common?cmd=reader&rid=" + rid + "&tid=" + tid);
		}else if (cmd.equals("submiting")) {// 提交车站准备数据
			Users user = (Users)request.getSession().getAttribute("manager");			
			request.setAttribute("train", this.findTrain(user.getTid()));
			request.getRequestDispatcher("web/sworks/submit.jsp").forward(request, response);
		}else if (cmd.equals("submit")) {// 提交车站准备数据		
			if(addFinish(request))
			{
				if(updateTrain(request)){
					request.setAttribute("msg", "恭喜你，提交成功!");
				}else{
					request.setAttribute("msg", "对不起，提交失败!");
				}
			}else{
				request.setAttribute("msg", "对不起!车站的信息没有添加或者修改完成,本次提交失败!");
			}   
			request.getRequestDispatcher("sworks?cmd=submiting").forward(request, response);
		}else if (cmd.equals("word")) {// 导出数据到word
			Users user = (Users)request.getSession().getAttribute("manager");	
			Trains train = this.findTrain(user.getTid());
			if(train.getTstate()==2){//tstate表示“已通过审核”
				request.setAttribute("navlist", rulesServices.findNavToWord(user.getTid()));
				request.getRequestDispatcher("common/htmltoword.jsp").forward(request, response);
			}else{
				request.getRequestDispatcher("common/tips.jsp").forward(request, response);
			}
		}else if (cmd.equals("update_admin")) {//管理员的提交
			int rid = Integer.parseInt(request.getParameter("rid"));
			String tid = request.getParameter("tid");
			String msg = "";
			String sid = request.getParameter("sid");
			if (sid.equals("0")||sid.equals("")|| sid == null) {
				if (this.addAdmin(request)) {
					msg = "恭喜你，你已经成功录入了车站的规章制度！";
				} else {
					msg = "对不起，你添加失败！";
				}
			} else {
				if (this.updateAdmin(request)) {
					msg = "恭喜你，你已经成功修改了车站的规章制度！";
				} else {
					msg = "对不起，你修改失败！";
				}
			}
			request.getSession().setAttribute("msg", msg);
			response.sendRedirect("common?cmd=reader&rid=" + rid + "&tid=" + tid);
		}else if (cmd.equals("input_ck")) {//参考章程的编写
			int tid = Integer.parseInt(request.getParameter("tid"));
			request.setAttribute("tid", tid);
			Trains cankaotrain = trainsServices.findTrain("SELECT * FROM Trains WHERE tid = " + tid);
			SworkRuleVO ruleswork = readRuleSwork(request);
			request.setAttribute("ruleswork",ruleswork);
			
			String tittleString = "编辑参考章程";
			if (ruleswork.getSwork() == null) {
				tittleString = "添加参考章程";
			}
			request.setAttribute("content_tittle", tittleString + " 属于车站：" + cankaotrain.getTname());
			request.getRequestDispatcher("web/sworks/input_ck.jsp").forward(request, response);
		}else if (cmd.equals("submit_ck")) {//参考章程的编写
			String tid = request.getParameter("tid");
			String msg = "";
			String sid = request.getParameter("sid");
			if (sid.equals("0")||sid.equals("")|| sid == null) {
				if (this.addCK(request)) {
					msg = "恭喜你，你已经成功录入了车站规章制度的参考章程！";
				} else {
					msg = "对不起，你添加失败！";
				}
			} else {
				if (this.updateCK(request)) {
					msg = "恭喜你，你已经成功修改了车站规章制度的参考章程！";
				} else {
					msg = "对不起，你修改失败！";
				}
			}
			request.getSession().setAttribute("msg", msg);
			response.sendRedirect("rules?cmd=list_sh&tid=" + tid);
		}
	}
	
	// 添加章程信息
	private boolean addSworkScontent(HttpServletRequest request) {
		int rid = Integer.parseInt(request.getParameter("rid"));
		String scontent = request.getParameter("scontent");
		String state = request.getParameter("state");
		String stime = request.getParameter("stime");
		String SQL = "INSERT INTO sworks(rid,tid,scontent,state,saddtime) VALUES("
				+ rid + "," 
				+ ((Users)request.getSession().getAttribute("manager")).getTid() + ",'"
				+ scontent
				+ "'," + state + ",'"
				+ new Today().getAllDateOfNum() + "')";
		return sworksServices.addSwork(SQL);
	}
	
	// 添加参考章程
	private boolean addSworkScommon(HttpServletRequest request) {
		int rid = Integer.parseInt(request.getParameter("rid"));
		String scommon = request.getParameter("scommon");
		String SQL = "INSERT INTO sworks(rid,tid,scommon,saddtime) VALUES("
				+ rid + "," 
				+ ((Users)request.getSession().getAttribute("manager")).getTid() + ",'"
				+ scommon
				+ "','"
				+ new Today().getAllDateOfNum() + "')";
		return sworksServices.addSwork(SQL);
	}
	
	// 添加参考章程_管理员添加
	private boolean addCK(HttpServletRequest request) {
		int rid = Integer.parseInt(request.getParameter("rid"));
		String tid = request.getParameter("tid");
		String scommon = request.getParameter("scommon");
		String SQL = "INSERT INTO sworks(rid,tid,scommon,ssstate,sdstate,saddtime) VALUES("
				+ rid + "," 
				+ tid + ",'"
				+ scommon
				+ "',0,0,'"
				+ new Today().getAllDateOfNum() + "')";
		return sworksServices.addSwork(SQL);
	}
	
	// 修改站务信息
	private boolean updateSworkScontent(HttpServletRequest request) {
		int sid = Integer.parseInt(request.getParameter("sid"));
		String scontent = request.getParameter("scontent");
		String state = request.getParameter("state");
		String stime = request.getParameter("stime");
		String SQL = "UPDATE sworks SET state = " + state + ",stime = " + stime + ",scontent = '" + scontent + "',supdatetime = '"
		+ new Today().getAllDateOfNum() + "' WHERE sid = " + sid;
		
		return sworksServices.updateSwork(SQL);
	}
	
	// 修改参考章程
	private boolean updateSworkScommon(HttpServletRequest request) {
		int sid = Integer.parseInt(request.getParameter("sid"));
		String scommon = request.getParameter("scommon");
		
		String SQL = "UPDATE sworks SET ssstate = 1,scommon = '"
		+ scommon + "',supdatetime = '"
		+ new Today().getAllDateOfNum() + "' WHERE sid = " + sid;
		
		return sworksServices.updateSwork(SQL);
	}
	
	// 修改参考章程_管理员修改
	private boolean updateCK(HttpServletRequest request) {
		int sid = Integer.parseInt(request.getParameter("sid"));
		String scommon = request.getParameter("scommon");
		
		String SQL = "UPDATE sworks SET scommon = '"
		+ scommon + "',supdatetime = '"
		+ new Today().getAllDateOfNum() + "' WHERE sid = " + sid;
		
		return sworksServices.updateSwork(SQL);
	}
	
	// 审核组审核
	private boolean updateSworkSsstate(HttpServletRequest request) {
		int sid = Integer.parseInt(request.getParameter("sid"));
		String ssadvice = request.getParameter("ssadvice");
		String sdstate = request.getParameter("sdstate");
		String ssstate = request.getParameter("ssstate");
		
		int state = 1;
		if (ssstate.equals("2")||sdstate.equals("2")) {
			 state = 0;
		}
		
		String SQL = "UPDATE sworks SET ssstate = " + ssstate + " ,state= " + state + ",ssadvice = '"
		+ ssadvice + "',supdatetime = '"
		+ new Today().getAllDateOfNum() + "' WHERE sid = " + sid;
		
		return sworksServices.updateSwork(SQL);
	}
	
	// 段长组审核
	private boolean updateSworkSdstate(HttpServletRequest request) {
		int sid = Integer.parseInt(request.getParameter("sid"));
		String sdadvice = request.getParameter("sdadvice");
		String sdstate = request.getParameter("sdstate");
		String ssstate = request.getParameter("ssstate");
		
		int state = 1;
		if (sdstate.equals("2")||ssstate.equals("2")) {
			 state = 0;
		}
		
		String SQL = "UPDATE sworks SET sdstate = " + sdstate + " ,state= " + state + ",sdadvice = '"
		+ sdadvice + "',supdatetime = '"
		+ new Today().getAllDateOfNum() + "' WHERE sid = " + sid;
		
		return sworksServices.updateSwork(SQL);
	}
	
	// 车站信息
	private List<Trains> listTrain(HttpServletRequest request) {
		String SQL = "SELECT * FROM Trains WHERE tstate !=10000  ORDER BY tid ASC";
		return trainsServices.findTrains(SQL);
	}
	
	// 添加火车站信息
	private Trains findTrain(int tid) {
		String SQL = "SELECT * FROM Trains WHERE tid = " + tid;
		return trainsServices.findTrain(SQL);
	}
	
	// 判断车站的数据是否录入完成
	private boolean addFinish(HttpServletRequest request) {
		int tid = ((Users)request.getSession().getAttribute("manager")).getTid();
		int rnums = rulesServices.findRules("SELECT * FROM Rules WHERE rlevel=4 ").size();	
		int snums = sworksServices.findSworks("SELECT * FROM Sworks WHERE scontent IS NOT NULL AND ssstate!= 2 AND tid = " + tid).size();
		return (rnums==snums)?true:false;
	}
	
	// 修改车站信息
	private boolean updateTrain(HttpServletRequest request) {
		int tid = ((Users)request.getSession().getAttribute("manager")).getTid();
		String SQL = "UPDATE Trains SET tstate = 1 WHERE tid = " + tid;
		return trainsServices.updateTrain(SQL);
	}
	
	// 分页查询规章编制信息
	private PageModel listRuleSwork(HttpServletRequest request,int tid) {
		int offset = 0;
		try {
			offset = Integer.parseInt(request.getParameter("pager.offset"));
		} catch (NumberFormatException e) {
			offset = 0;
		}
		String SQL = "SELECT * FROM Rules WHERE rlevel = 4 ORDER BY rid ASC";
		return rulesServices.listRuleSwork(offset,10,SQL,tid);
	}
	// 阅读规章制度_徐
	private SworkRuleVO readRuleSwork(HttpServletRequest request) {
		SworkRuleVO lsrvoSworkRuleVO = new SworkRuleVO();
		Learns learn = null;
		int rid = Integer.parseInt(request.getParameter("rid"));
		int tid = Integer.parseInt(request.getParameter("tid"));

		String SQLBace = "SELECT * FROM Rules WHERE rlevel=4 ";
		Rules first = rulesServices.findRule(SQLBace
				+ " order by rid asc limit 0,1 ");
		Rules last = rulesServices.findRule(SQLBace
				+ " order by rid desc limit 0,1 ");

		Rules rules = rulesServices.findRule("SELECT * FROM Rules WHERE rid = "
				+ rid);
		if (rules.getRid() == first.getRid()) {
			lsrvoSworkRuleVO.setFirstLast("first");
		} else if (rules.getRid() == last.getRid()) {
			lsrvoSworkRuleVO.setFirstLast("last");
		} else {
			lsrvoSworkRuleVO.setFirstLast("");
		}

		Sworks sworks = sworksServices.findSwork("SELECT * FROM Sworks WHERE tid = " + tid
						+ " and rid = " + rid);
		lsrvoSworkRuleVO.setSwork(sworks);
		lsrvoSworkRuleVO.setRule(rules);

		return lsrvoSworkRuleVO;
	}
	
	// 添加章程信息 admin
	private boolean addAdmin(HttpServletRequest request) {
		int rid = Integer.parseInt(request.getParameter("rid"));
		String tid = request.getParameter("tid");
		String scontent = request.getParameter("scontent");
		String scommon = request.getParameter("scommon");
		String sdadvice = request.getParameter("sdadvice");
		String ssadvice = request.getParameter("ssadvice");
		String ssstate = request.getParameter("ssstate");
		String sdstate = request.getParameter("sdstate");
		String SQL = "INSERT INTO sworks(rid,tid,scontent,scommon,sdadvice,ssadvice,ssstate,sdstate,saddtime) VALUES("
				+ rid + "," 
				+ tid + ",'"
				+ scontent + "','" 
				+ scommon + "','"
				+ sdadvice + "','"
				+ ssadvice + "',"
				+ ssstate + ","
				+ sdstate + ",'"
				+ new Today().getAllDateOfNum() + "')";
		return sworksServices.addSwork(SQL);
	}
	
	// 修改站务信息 admin
	private boolean updateAdmin(HttpServletRequest request) {
		int sid = Integer.parseInt(request.getParameter("sid"));
		String scontent = request.getParameter("scontent");
		String scommon = request.getParameter("scommon");
		String sdadvice = request.getParameter("sdadvice");
		String ssadvice = request.getParameter("ssadvice");
		String ssstate = request.getParameter("ssstate");
		String sdstate = request.getParameter("sdstate");
		String SQL = "UPDATE sworks SET scontent = '" + scontent 
					+ "',scommon = '" + scommon 
					+ "',sdadvice = '" + sdadvice 
					+ "',ssadvice = '" + ssadvice
					+ "',ssstate = " + ssstate 
					+ ",sdstate = " + sdstate
					+ ",supdatetime = '"
		+ new Today().getAllDateOfNum() + "' WHERE sid = " + sid;
		
		return sworksServices.updateSwork(SQL);
	}
}
